package com.sf.guava.basic;

import com.google.common.base.MoreObjects;
import com.google.common.base.Objects;
import com.google.common.collect.ComparisonChain;
import com.google.common.collect.Ordering;

/**
 * @Classname ObjectsTest
 * @Description Objects 使用
 * @Date 2019/12/15 0015 17:48
 * @Created by 埔枘
 */
public class ObjectsTest {

    public static void main(String[] args) {
        Person person1 = new Person();
        person1.setFirstName("321");
        person1.setLastName("123");
        person1.setZipCode(3);
        Person person2 = new Person();
        person2.setFirstName("321");
        person2.setLastName("123");
        person2.setZipCode(2);

        System.out.println(person1.compareTo(person2));
    }

    /**
     *  1. 返回两个参数中 不为空的一个
     *  2. 如果都是空则抛出NullPointerException。
     *  3. 如果两个都不为空 则返回 第一个
     *
     *   作用 定制 toString
     */
    public static void MoreObjectsTest(){
        System.out.println(MoreObjects.firstNonNull("321","123"));
        User user = new User();
        MoreObjects.ToStringHelper result = MoreObjects.toStringHelper(user)
                .add("age", "18")
                .add("userName", null);
        result.addValue("ok");
        System.out.println(result);
        //去除空值
        result.omitNullValues();
        System.out.println(result);
    }

    /**
     *  Objects 方法 使用
     */
    public static void objectsTest(){
        String str1 = "123";
        String str2 = new String("123");

        // 检查 可能为空 的对象时 不会出现 NullPointException
        System.out.println(Objects.equal("a", "a"));
        System.out.println(Objects.equal(null, "a"));
        System.out.println(Objects.equal("a", null));
        System.out.println(Objects.equal(null, null));
        System.out.println(Objects.equal(str1, str2));
    }

}

class Person implements Comparable<Person>{
    private String lastName;
    private String firstName;
    private int zipCode;

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public int getZipCode() {
        return zipCode;
    }

    public void setZipCode(int zipCode) {
        this.zipCode = zipCode;
    }

    @Override
    public int compareTo(Person other) {
         //JDK 方式 实现
//        int cmp = lastName.compareTo(other.lastName);
//        if (cmp != 0) {
//            return cmp;
//        }
//        cmp = firstName.compareTo(other.firstName);
//        if (cmp != 0) {
//            return cmp;
//        }
//        return Integer.compare(zipCode, other.zipCode);
        // 通过 比较链的方式 比较
        return ComparisonChain.start()
                .compare(this.lastName, other.lastName)
                .compare(this.firstName, other.firstName)
                .compare(this.zipCode, other.zipCode, Ordering.natural().nullsLast())
                .result();
    }
}
